package com.cloud.force.api.web;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.force.api.bean.Part;
import com.cloud.force.api.entity.TaskListPart;
import com.cloud.force.api.service.TaskListPartService;
import com.cloud.force.common.context.HumanSession;
import com.cloud.force.common.result.Quokka;
import com.cloud.force.common.utils.SessionHelper;
import com.cloud.force.interfaces.FeignClientFreeflow;

@RestController
@RequestMapping("force/api/tasklist/part")
public class TaskListPartController {

	@Autowired
	private TaskListPartService taskListPartService;

	@Autowired
	private FeignClientFreeflow feignFreeflowClient;

	@RequestMapping("/listprocs")
	public Quokka listProcsByBizId(long bizId) {
		return new Quokka(true, feignFreeflowClient.listProcDefByBizId(bizId));
	}

	@RequestMapping("/list")
	public Quokka listPartsByProcDefId(long taskListId, long procDefId) {
		List<Part> parts = feignFreeflowClient.loadPartsByProcDef(procDefId);
		List<TaskListPart> taskListParts = taskListPartService.listByTaskListProcDef(taskListId, procDefId);
		for (TaskListPart tlp : taskListParts) {
			for (Part p : parts) {
				if (tlp.getPartId().equals(p.getPartId())) {
					p.setCode(1);
				}
			}
		}
		return new Quokka(true, parts);
	}

	@RequestMapping("saveupdate")
	public Quokka saveUpdateTaskListParter(long procDefId, long taskListId, String partIds) {
		return new Quokka(true, taskListPartService.saveUpdateTaskListPart(procDefId, taskListId, partIds));
	}

	@RequestMapping("listbybizpart")
	public Quokka listTaskList(String taskListIds) {
		List<Long> parts = new ArrayList<Long>();// 通过部门，岗位，人员找到对应的箱子
		HumanSession session = SessionHelper.get();
		parts.add(0L);
		parts.add(session.getHumanId());
		parts.add(session.getUnitId());
		if(null != session.getRoleIds()){
			parts.addAll(session.getRoleIds());
		}
		return new Quokka(true, taskListPartService.listTaskListByPart(taskListIds, parts));
	}
	
}
